package com.contact.util.util.validation;

import com.contact.util.StringUtils;

public class RequiredPropertyValidator implements PropertyValidator {

    private final transient String property;

    public RequiredPropertyValidator(String property) {
        this.property = property;
    }

    @Override
    public String getProperty() {
        return property;
    }

    @Override
    public ValidationError validate(Object value) {
        if (value == null) {
            return new ValidationError(String.format("Required property '%s' cannot be null.", property));
        }
        if (value.getClass().isAssignableFrom(String.class)) {
            String sValue = (String) value;
            if (StringUtils.isEmpty(sValue)) {
                return new ValidationError(String.format("Required property '%s' cannot be empty.", property));
            }
        }
        return null;
    }

}
